package com.dzm.xgb.sms.controller.api;

import com.dzm.common.exception.Assert;
import com.dzm.common.result.R;
import com.dzm.common.result.ResponseEnum;
import com.dzm.common.util.RegexValidateUtils;
import com.dzm.xgb.sms.client.UserInfoClient;
import com.dzm.xgb.sms.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 作者：Dzm
 * 时间：2024-08-29-11:27
 * 功能：
 */

@RestController
@RequestMapping("/api/sms")
@Api(tags = "短信管理")
public class ApiSmsController {

    @Autowired
    private SmsService smsService;

    @Autowired
    private UserInfoClient userInfoClient;

    @ApiOperation("发送短信")
    @GetMapping("/send/{phoneNumber}")
    public R sendMsg(@PathVariable("phoneNumber") String phoneNumber) throws Exception {
        R r = userInfoClient.checkPhone(phoneNumber);
        Assert.isTrue(r.getCode().equals(0), ResponseEnum.FEIGN_CLIENT_ERROR);
        boolean isExist = (boolean) r.getData().get("isExist");
        Assert.isTrue(!isExist, ResponseEnum.MOBILE_EXIST_ERROR);

        //手机号不能为空
        Assert.notNull(phoneNumber, ResponseEnum.MOBILE_NULL_ERROR);
        //验证手机号格式
        Assert.isTrue(RegexValidateUtils.checkCellphone(phoneNumber), ResponseEnum.MOBILE_ERROR);

        String code = smsService.sendMsg(phoneNumber);

        return R.ok().data("code", code);
    }
}
